package edu.npu.cv.controller;

import edu.npu.cv.constant.TemplatePath;
import edu.npu.cv.dto.ActivityDTO;
import edu.npu.cv.entity.ArticleDO;
import edu.npu.cv.service.PaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lelechang
 * @description: 控制器层
 * @version: V1.0
 */
@RestController
@RequestMapping(TemplatePath.S)
public class IndexController {

    @Autowired
    private PaService paService;

    /**
     * 从数据库获取公告列表 前十条
     */
    private List<ArticleDO> getPAList() {
        return paService.findPaIndex();
    }

    /**
     * 查询所有主页需要的数据
     *
     * @return
     */
    @GetMapping
    public ModelAndView list(Model model) {
        List<ArticleDO> articleDOList = getPAList();
        List<ActivityDTO> activityDTOList = new ArrayList<>();
        List<ArticleDO> list1 = new ArrayList<>();
        for (int i = 0; i < articleDOList.size(); i++) {
            activityDTOList.add(i, ActivityDTO.ActivityDO2DTO(articleDOList.get(i)));
        }
        if (articleDOList.size()>0){
             list1 = articleDOList.subList(0, 1);
        }

        if(articleDOList.size()>4){
            articleDOList = articleDOList.subList(1, 6);
        }
        model.addAttribute("paList", articleDOList);
        model.addAttribute("paList1", list1);
        model.addAttribute("paList2", activityDTOList);
        return new ModelAndView("index", "paModel", model);
    }
}
